package com.example.demo.controller;

import com.example.demo.entity.RotateEntity;
import com.example.demo.service.RotateService;
import com.example.demo.tool.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.jfr.Registered;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "轮播图")
public class RotateController {
    @Autowired
    private RotateService rotateService;

    @PostMapping("rotation/add")
    @ApiOperation("增加")
    public ResponseResult add(RotateEntity rotateEntity,@RequestPart("file") MultipartFile file) throws Exception{
        String originalFilename = null;
        File doc=null;
        if(!file.isEmpty()){
            originalFilename = file.getOriginalFilename();//获取文件原始文字
            doc=new File("D:\\xu\\img\\"+originalFilename);
            file.transferTo(doc);//上传到服务器地址
        }
        rotateEntity.setImg("img/"+originalFilename);
        rotateEntity.setCreateTime(new Date());
        rotateService.save(rotateEntity);
        return ResponseResult.success();
    }

    @GetMapping("allrotation")
    @ApiOperation("获取全部轮播图数据")
    public ResponseResult listAll(){
        List<RotateEntity> list = rotateService.list();
        return ResponseResult.success(list);
    }

    @PostMapping("edit/rotation")
    @ApiOperation("根据id修改轮播图")
    public ResponseResult edit(Integer id,RotateEntity rotateEntity){
        rotateService.updateById(rotateEntity);
        return ResponseResult.success();
    }
}
